﻿using CheatMenu.Managers;
using System.Collections.Generic;
using UnityEngine;
using CheatMenu.Utils;
using Logger = BepInEx.Logger;
using BepInEx.Logging;

namespace CheatMenu.Menus
{
    public class MenuConfig : MenuBase
    {
        private string minControlHeight = GUIManager.MinControlHeight.ToString();
        //private string proposedItemCount = GUIManager.ProposedItemCount.ToString();
        private float proposedItemCountSlider = GUIManager.ProposedItemCount;
        private Vector2 bindsViewVector;
        public void ResetScrollVector() => bindsViewVector = new Vector2();

        private GUIStyle defaultFontNoWrapLabelStyle;
        private GUIStyle defaultFontControlHeightButtonStyle;
        private GUIStyle defaultFontTextFieldStyle;
        private GUIStyle scaleSliderStyle;
        private GUIStyle scaleSliderThumbStyle;
        private GUIStyle stretchWidthHeightBoxStyle;
        private GUIStyle commandButtonStyle;

        MenuPromtKeyBind keyBindPrompt;

        public MenuConfig() : base("Config")
        {
            Alignment = Alignment.Centre;

            keyBindPrompt = new MenuPromtKeyBind("Key Binding");
            keyBindPrompt.KeyBoundEvent += (s, e) =>
            {
                Keybind keyBind = BindManager.GetKeyBind(keyBindPrompt.BindName);
                if (e == KeyCode.Escape)
                {
                    if (keyBind.CanUnbind)
                    {
                        keyBind.KeyCode = KeyCode.None;
                    }
                    else
                    {
                        MenuManager.GetMenu<MenuNotifications>().PushNotification("Invalid Action", "Can't unbind this key!");
                    }
                }
                else
                {
                    BindManager.IgnoreNext = true;
                    keyBind.KeyCode = e;
                }
            };
        }

        protected override Vector2 InitializeSize()
        => new Vector2((GUIManager.ControlWidth * 2) + (GUIManager.PaddingSize * 2), (GUIManager.ControlHeight * 15) + (GUIManager.PaddingSize * 2));

        protected override void InitializeGUI()
        {
            defaultFontNoWrapLabelStyle = new GUIStyle(GUI.skin.label)
            {
                fontSize = GUIManager.ControlFontSize,
                wordWrap = false,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelColor
                }
            };

            defaultFontControlHeightButtonStyle = new GUIStyle(GUI.skin.button)
            {
                fontSize = GUIManager.ControlFontSize,
                fixedHeight = GUIManager.ControlHeight
            };

            defaultFontTextFieldStyle = new GUIStyle(GUI.skin.textField)
            {
                fontSize = GUIManager.ControlFontSize
            };

            scaleSliderStyle = new GUIStyle(GUI.skin.horizontalSlider)
            {
                fixedHeight = GUIManager.ControlHeight
            };

            scaleSliderThumbStyle = new GUIStyle(GUI.skin.horizontalSliderThumb)
            {
                fixedHeight = GUIManager.ControlHeight,
                fixedWidth = GUIManager.ControlHeight
            };

            stretchWidthHeightBoxStyle = new GUIStyle(GUI.skin.box)
            {
                stretchWidth = true,
                stretchHeight = true
            };

            commandButtonStyle = new GUIStyle(GUI.skin.button)
            {
                margin = new RectOffset(0, 0, 0, 0),
                padding = new RectOffset(0, 0, 0, 0),
                fontSize = GUIManager.ControlFontSize,
                fixedHeight = GUIManager.ControlHeight
            };
        }

        protected override void OnShown()
        {
            minControlHeight = GUIManager.MinControlHeight.ToString();
            proposedItemCountSlider = GUIManager.ProposedItemCount;
        }

        protected override void OnHidden()
        {
            keyBindPrompt.Shown = false;
        }

        protected override void MenuWindow(int id)
        {
            GUILayout.BeginVertical();
            {
                GUILayout.BeginHorizontal();
                {
                    GUILayout.FlexibleSpace();
                    GUILayout.BeginHorizontal(GUI.skin.box);
                    {
                        if (GUILayout.Button("Apply", defaultFontControlHeightButtonStyle))
                        {
                            bool dirty = ParseInputAndIsDirty();
                            if (dirty)
                            {
                                GUIManager.CalculateScaling();
                                MenuManager.ReloadMenuGUI();
                            }
                        }
                        if (GUILayout.Button("Close", defaultFontControlHeightButtonStyle))
                        {
                            Shown = false;
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginVertical(GUI.skin.box);
                {
                    float percent = (2f - (proposedItemCountSlider / GUIManager.DefaultProposedItemCount)) * 100f;
                    GUILayout.Label($"Scaling: {percent:0}%", defaultFontNoWrapLabelStyle);
                    proposedItemCountSlider = GUILayout.HorizontalSlider(proposedItemCountSlider, GUIManager.MaxProposedItemCount, GUIManager.MinProposedItemCount, scaleSliderStyle, scaleSliderThumbStyle);

                    GUILayout.Label("MinControlHeight", defaultFontNoWrapLabelStyle);
                    minControlHeight = GUILayout.TextField(minControlHeight, defaultFontTextFieldStyle);

                    GUILayout.Label("Binds", defaultFontNoWrapLabelStyle);
                    List<Keybind> keybinds = BindManager.Keybinds;

                    bindsViewVector = GUILayout.BeginScrollView(bindsViewVector, false, false, GUI.skin.horizontalScrollbar, GUI.skin.verticalScrollbar, stretchWidthHeightBoxStyle);
                    for (int i = 0; i < keybinds.Count; i++)
                    {
                        Keybind bind = keybinds[i];
                        if (GUILayout.Button($"{bind.Name} ({bind.KeyCode})", commandButtonStyle))
                            keyBindPrompt.PopulateAndShow(bind.Name);
                    }
                    GUILayout.EndScrollView();
                }
                GUILayout.EndVertical();
            }
            GUILayout.EndVertical();
        }

        private bool ParseInputAndIsDirty()
        {
            bool dirty = false;

            if (int.TryParse(minControlHeight, out int outMinControlHeight))
            {
                if (outMinControlHeight < GUIManager.MinMinControlHeight || outMinControlHeight > GUIManager.MaxMinControlHeight)
                {
                    outMinControlHeight = Mathf.Clamp(outMinControlHeight, GUIManager.MinMinControlHeight, GUIManager.MaxMinControlHeight);
                    minControlHeight = outMinControlHeight.ToString();
                }
                if (GUIManager.MinControlHeight != outMinControlHeight)
                {
                    GUIManager.MinControlHeight = outMinControlHeight;
                    dirty = true;
                }
            }
            else
            {
                minControlHeight = GUIManager.MinControlHeight.ToString();
            }

            int proposedItemCount = Mathf.RoundToInt(proposedItemCountSlider);
            if (GUIManager.ProposedItemCount != proposedItemCount)
            {
                GUIManager.ProposedItemCount = proposedItemCount;
                dirty = true;
            }

            return dirty;
        }
    }
}
